//
//  TMGPlanViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/12.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGPlanViewController.h"
#import "TMGPlanTableViewCell.h"
#import "TMGPlanModel.h"
#import "TMGPlanRecordViewController.h"
#import "TMGRansomViewController.h"
#import "TMGPlanRecordModel.h"
#import "TMGSmallPlanRecordViewController.h"
@interface TMGPlanViewController ()
@property(nonatomic, strong) NSMutableArray <TMGPlanRecordModel*>                    *dataSouce;
@property(nonatomic, strong) NSString     *allMoney;
@property(nonatomic, strong) NSString     *freeze;
@end

@implementation TMGPlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self sendPlanRequest];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 193)];
    bannerImageView.image = [UIImage imageNamed:@"wdebanner"];
    bannerImageView.userInteractionEnabled = YES;
    [header addSubview:bannerImageView];
    
    CGFloat money = 0;
    for (TMGPlanRecordModel * model in self.dataSouce) {
        money = money + model.assets.floatValue;
    }
    TMGPlanRecordModel * model = self.dataSouce.firstObject;
    
    NSString * freeze = @"";
    if (self.freeze.floatValue > 0) {
        freeze = [NSString stringWithFormat:@"(冻结%@)",self.freeze];
    }
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 10, UIScreenWidth - 30, 80)
                                                     text:[NSString stringWithFormat:@"投资计划(元)\n%@%@",
                                                           self.allMoney,freeze]
                                                textColor:[UIColor whiteColor]
                                                     font:24
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerX = UIScreenWidth / 2;
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"投资计划(元)" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    [bannerImageView addSubview:titleLable];
    
    header.height = bannerImageView.bottom;
    //头部橘色
    UIView * headers = [[UIView alloc] initWithFrame:CGRectMake(0, -header.height, UIScreenWidth, 2 * header.height)];
    headers.backgroundColor = TM_orangeColor;
    [header insertSubview:headers atIndex:0];
    
    
    self.tableView.tableHeaderView = header;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGPlanTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGPlanTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGPlanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    __weak typeof(self) weakSelf =  self;
    TMGPlanRecordModel * model = self.dataSouce[indexPath.section];
    [cell.buyButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedRecicalButtonWithIndexPath:indexPath];
    }];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    TMGPlanRecordModel * model = self.dataSouce[indexPath.section];
    //安心计划
    TMGPlanRecordViewController * vc = [[TMGPlanRecordViewController alloc] init];
    NSString * type;
    if ([model.name containsString:@"零钱"]) {
        type = @"5";
        [self.navigationController pushViewController:[TMGSmallPlanRecordViewController new] animated:YES];
        return;
    }
    if ([model.name containsString:@"安心"]) {
        type = @"6";
    }
    if ([model.name containsString:@"月月"]) {
        type = @"7";
    }
    vc.type = type;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)onSelctedRecicalButtonWithIndexPath:(NSIndexPath*)indexPath{
    TMGPlanRecordModel * model = self.dataSouce[indexPath.section];
    TMGRansomViewController * vc = [TMGRansomViewController new];
    vc.money = model.assets;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendPlanRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/investmentPlan"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * allDic = array.firstObject;
                                                 self.allMoney = [allDic[@"assets"] description];
                                                 self.freeze = [allDic[@"freeze"] description];
                                                 for (NSDictionary * modelDic in array) {
                                                     TMGPlanRecordModel * mdoel = [TMGPlanRecordModel mj_objectWithKeyValues:modelDic];
                                                     if (mdoel.recordID.integerValue != 1) {
                                                         if ([mdoel.name containsString:@"零钱"]) {
                                                             mdoel.canbuy = YES;
                                                             [self.dataSouce insertObject:mdoel atIndex:0];
                                                         }else{
                                                             [self.dataSouce addObject:mdoel];
                                                         }
                                                     }

                                                     
                                                 }
                                                 [self configSubView];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end

